package edu.vt.sdm.tcga.filenavigator.parser;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import edu.vt.sdm.tcga.filenavigator.parser.clinical.ClinicalAliquot;
import edu.vt.sdm.tcga.filenavigator.parser.clinical.ClinicalAnalyte;
import edu.vt.sdm.tcga.filenavigator.parser.clinical.ClinicalPortion;
import edu.vt.sdm.tcga.filenavigator.parser.clinical.ClinicalProtocol;
import edu.vt.sdm.tcga.filenavigator.parser.clinical.ClinicalSample;
import edu.vt.sdm.tcga.filenavigator.parser.clinical.ClinicalSlide;
import edu.vt.sdm.tcga.filenavigator.parser.expression.level1.ExpressionFeatures;
import edu.vt.sdm.tcga.filenavigator.parser.expression.level1.ExpressionParams;
import edu.vt.sdm.tcga.filenavigator.parser.expression.level1.ExpressionStats;
import edu.vt.sdm.tcga.filenavigator.parser.expression.level2.LowessNormalized;
import edu.vt.sdm.tcga.filenavigator.parser.expression.level3.Analysis;
import edu.vt.sdm.tcga.filenavigator.parser.methylation.level1.Methylation;
import edu.vt.sdm.tcga.filenavigator.parser.methylation.level2.MethylationSignalIntensity;
import edu.vt.sdm.tcga.filenavigator.parser.methylation.level3.MethylationAnalysis;
import edu.vt.sdm.tcga.filenavigator.parser.snp.level3.SNPAnalysis;

public class HibernateUtil {

	private static final SessionFactory sessionFactory;
	static {
		try {
			System.out.println("Setting up session...");
			sessionFactory = new AnnotationConfiguration().configure()
					.addPackage("edu.vt.sdm.tcga.filenavigator.parser")
					.addAnnotatedClass(Analysis.class)
					.addAnnotatedClass(LowessNormalized.class)
					.addAnnotatedClass(ExpressionParams.class)
					.addAnnotatedClass(ExpressionStats.class)
					.addAnnotatedClass(ExpressionFeatures.class)

					.addAnnotatedClass(Methylation.class)
					.addAnnotatedClass(MethylationSignalIntensity.class)
					.addAnnotatedClass(MethylationAnalysis.class)

					.addAnnotatedClass(ClinicalAliquot.class)
					.addAnnotatedClass(ClinicalAnalyte.class)
					.addAnnotatedClass(ClinicalPortion.class)
					.addAnnotatedClass(ClinicalProtocol.class)
					.addAnnotatedClass(ClinicalSample.class)
					.addAnnotatedClass(ClinicalSlide.class)

					.addAnnotatedClass(SNPAnalysis.class)

					.buildSessionFactory();
		} catch (Throwable ex) {
			// Log exception!
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static Session getSession() throws HibernateException {
		System.out.println("Getting a Hibernate session...");
		return sessionFactory.openSession();
	}
}
